 /*
 * UTILITY VARIE COMMONS HS
 *
 */


 

    
  
  //start tabelle
  function startTable(t, base_url, url){
      
         if(!base_url)
            baseurl = $(t).attr("mb:baseurl");
        else
            baseurl = base_url;
        
      
           $(t).dataTable( {
            "bProcessing": true,
            "bServerSide": true,
            "sAjaxSource": baseurl + url,
            "iDisplayLength": 10,
            "bAutoWidth": false,
            "sPaginationType": "full_numbers",
            "bLengthChange": false,
            "bFilter": false 
        } );
      
  }  
    
  function addTags(t,base_url){
      
        if(!base_url)
            baseurl = $(t).attr("mb:baseurl");
        else
            baseurl = base_url;
  
        $(t).tagsInput({

                autocomplete_url:  baseurl + 'tag/json/search'
       });
  }
  
  function addLike() {
      
            //like generico sugli object
            $(".like").click(function() {
                    url_to = this.getAttribute("mb:url");
                    $.ajax({
                      url: url_to,
                      context: document.body
                    }).done(function() { 
                        //qui refresh della scritta e tolgo il click
                        $('.like').text('Like!!');
                        $('.like').off('click');
                    });
             });
  }
  
 
   function saveComment() {
            
            $("#submitcomment").click(function() {
                    url_to = this.getAttribute("mb:url");
                    url_action = this.getAttribute("mb:action");
                    objtype = this.getAttribute("mb:objtype");
                    objid = this.getAttribute("mb:objid");
                    description = $(".descriptioncomment");
                     
                    return;
                    $.ajax({
                      url: url_action,
                      type: 'POST',
                      data: { description: "test", "objtype" : objtype, "objid" : objid },
                      success: function(data) {
                                    document.location.href=url_to;
                                }
                     
                     });

                });
   }
  

    function log( message ) {
            $( "<div/>" ).text( message ).prependTo( "#log" );
            $( "#log" ).scrollTop( 0 );
    };

 
 
    
 /*
 * form di registrazione utente
 *
 */   
    function userAddForm(){
    
    
      if(!$("#userAddForm")) 
          return;

  
       $( "#location" ).autocomplete({
                source: function( request, response ) {
                    $.ajax({
                                url: /* $("#userAddForm").attr("mb:baseurl") + */ "/geonames/json/search",
                                dataType: "json",
                                data: {
                                        term: request.term
                                },

                                success: function( data ) {
                                        response( $.map( data.geonames, function( item ) {

                                                $( "#coordx" ).val(item.latitude);
                                                $( "#coordy" ).val(item.longitude);

                                                return {
                                                        label: item.name,
                                                        value: item.name
                                                }
                                        }));
                                }
                        });
                },
                minLength: 2,
                select: function( event, ui ) {

                                console.log( ui.item ?
                                    "Selected: " + ui.item.label :
                                    "Nothing selected, input was " + this.value);

                },
                open: function() {
                        $( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
                },
                close: function() {
                        $( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
                }
        });



        
            //verifica se il nome utente non esiste 
       $("#username").keyup(function(event,baseurl){
            var username = $("#username").val();

            if(username.length < 6) {
               
            }else{

                $('#username_availability_result').html('start checking'); 

                $.ajax({
                            url: $("#userAddForm").attr("mb:baseurl") + "/user/json/usernameexists",
                            dataType: "json",
                            data: {
                                    username: username
                            },

                            success: function( data ) {

                                        if(data.usernameexists == true)
                                                $('#username_availability_result').html('username already exists!!'); 
                                        else
                                            $('#username_availability_result').html('username ok'); 
                            }
                         }
                );
           }


        });
        
        
         //verifica se la email non è usata utente non esiste 
         $("#email").keyup(function(event){
                var email = $("#email").val();

                if(email.length < 4) {
                   
                }else{

                    $('#email_availability_result').html('checking'); 

                    $.ajax({
                                        url: $("#userAddForm").attr("mb:baseurl") + "/user/json/emailexists",
                                        dataType: "json",
                                        data: {
                                                email: email
                                        },

                                        success: function( data ) {


                                                    if(data.emailexists == true)
                                                            $('#email_availability_result').html('email already exists!!'); 
                                                    else
                                                        $('#email_availability_result').html('email ok'); 



                                        }
                             }
                    );
               }


         });
            
        

        $("#userAddForm").validate({
            
            rules : {
                        firstname : "required",
                        
                        lastname : "required",
                        
                        username : {
                            required: true,
                            minlength: 6
                        },
                        
                        email : {
                            required: true,
                            email: true
                        },
                        
			password: {
				required: true,
				minlength: 5
			},
                        
			checkpassword: {
				required: true,
				minlength: 5,
				equalTo: "#password"
			}
                        
                        
                
            },
		messages: {
			firstname: "Please enter your firstname",
			lastname:  "Please enter your lastname",
			username: {
				required:  "Please enter a username",
				minlength: "Your username must consist of at least 6 characters"
			},
			password: {
				required:  "Please provide a password",
				minlength: "Your password must be at least 5 characters long"
			},
			checkpassword: {
				required:  "Please provide a password",
				minlength: "Your password must be at least 5 characters long",
				equalTo:   "Please enter the same password as above"
			},
			email: "Please enter a valid email address"
		}
       
        });
   
    
    }






         $(document).ready(function() {


      
            
            addLike();
            addTags('#tags',null);

            userAddForm();

            //Tabelle            
            startTable('#tabletrack' , null, 'track/json/all');
            startTable('#tablegroup', null,  'group/json/all');
           
           //chiamata in ajax per salvare i commenti
           saveComment();



             //grafici
             /*foreach($k in $activities)
                $('#dashboardgraph-$k').visualize({type: 'bar', width: '420px'});
             end
             */






            $(".addtracktogroup").click(function() {
                    url_to = this.getAttribute("mb:url");
                    $.ajax({
                      url: url_to,
                      context: document.body
                    }).done(function() { 
                        //qui refresh della scritta e tolgo il click
                        $('.addtracktogroup').text('ok!!');
                        $('.addtracktogroup').off('click');
                    });
             });





            } );
            
            
